package com.ironman.imes.device.framework.base.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class MonitoredThreadPoolExecutor extends ThreadPoolExecutor {

    private final static ScheduledExecutorService MONITOR = Executors.newScheduledThreadPool(1);

    public MonitoredThreadPoolExecutor(String poolName, int nThreads) {
        super(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(), new ThreadFactory(poolName));
        MONITOR.scheduleAtFixedRate(() -> log.debug("PoolName:{}=>Active Threads:{}, Completed Tasks:{}, Pool Size: {}, Queue Size: {}", poolName, getActiveCount(), getCompletedTaskCount(), getPoolSize(), getQueue().size()), 0, 10, TimeUnit.SECONDS);
    }
}
